{% extends 'admin/admin_base.html' %}
{% block title %}{% if 'create' == cha[0] %}增加{% elif 'change' == cha[0] %}修改{% else %}查看{% endif %}-用户管理{% endblock %}
{% block head %}
    {{ super() }}
{% endblock %}
{% block body %}
    <ul class="nav nav-tabs">
        {% if 'change' == cha[0] %}
            <li onclick="chick_1()"><a href="#" >保存</a></li>
        {% elif 'create' == cha[0] %}
            <li onclick="chick()"><a href="#" >保存</a></li>
        {% endif %}

        <li><a href="{{ url_for('myview.index') }}">取消</a></li>
    </ul>
    <div class="admin-form form-horizontal">
        <div class="form-group">
            <label for="user" class="col-md-2 control-label">用户名</label>
            <div class="col-md-10" style="width: 30%">
            {% if 'change' == cha[0] or 'check' == cha[0]%}
            <input class="form-control" id="user" name="user" type="text" value="{{ cha[1]['user'] }}" readonly="true"></div>
            {% else %}
                <input class="form-control" id="user" name="user" type="text"></div>
            {% endif %}
        </div>
        <div class="form-group">
            <label for="password" class="col-md-2 control-label">密码</label>
            <div class="col-md-10" style="width: 30%">
            {% if 'change' == cha[0]%}
                <input class="form-control" id="password" name="password" type="password" readonly="true" value="123456"></div>
                    <button class="haha" value="重置" onclick="onon()">点击重置</button>
                <strong style="color: red">重置密码：123456</strong>
            {% elif 'check' == cha[0] %}
                <input class="form-control" id="password" name="password" type="password" readonly="true" value="123456"></div>
            {% else %}
                <input class="form-control" id="password" name="password" type="text" readonly="true" value="123456"></div>
            {% endif %}
        </div>
        <div class="form-group">
            <label for="group" class="col-md-2 control-label">组</label>
            <div class="col-md-10" style="width: 30%">
                {% if 'change' == cha[0] or 'check' == cha[0]%}
                    <input class="form-control" id="group" name="group" type="text" value="{{ cha[1]['group'] }}"></div>
                {% else %}
                    <input class="form-control" id="group" name="group" type="text" value="2"></div>
                {% endif %}

            <strong style="color: red">管理员：1  普通用户：2</strong>
        </div>
        <label class="col-md-2 control-label"></label>
            <div class="col-md-10" style="width: 30%">
                <p id="message1" style="color: red;"></p>
            </div>
    </div>
    <input id="photo" style="position: absolute; top: -9999px; left: -9999px;" value="{{ cha[0] }}"/>
{% endblock %}
{% block tail %}
        {{ super() }}
{#    <!--{{ lib.form_js() }}-->#}
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
    <script type="text/javascript">
        function chick() {
            var name = document.getElementById('user').value;
            var password = '123456';
            var group = document.getElementById('group').value;
            var gg = document.getElementById('photo').value;
            $.ajax({
                headers: {"X-CSRFToken": '{{ csrf_token }}'},
                url: "user_1/",
                type: 'POST',
                tradition: true,
                dataType: 'json',
                data: {'name': name,'password': password,'group':group,'cha':gg},
                success: function (data) {
                    if (data.state == 'success') {
                        document.getElementById("message1").innerHTML = '添加成功';
                        window.location.href = data.url
                    } else if (data.state == 'fail') {
                        $('#message1').html(data.message);
                        setTimeout(function () {
                            $('#message1').html('');
                        }, 3000)
                    }
                }
            })
        }
        function chick_1() {
            var name = document.getElementById('user').value;
            var pass = document.getElementById('password');
            if(pass.type == 'text'){
                var password = 123456;
            }else{
                var password = '';
            }
            var group = document.getElementById('group').value;
            var gg = document.getElementById('photo').value;
            $.ajax({
                headers: {"X-CSRFToken": '{{ csrf_token }}'},
                url: "user_1/",
                type: 'POST',
                tradition: true,
                dataType: 'json',
                data: {'name': name,'password': password,'group':group,'cha':gg},
                success: function (data) {
                    if (data.state == 'success') {
                        document.getElementById("message1").innerHTML = '登录成功';
                        window.location.href = data.url
                    } else if (data.state == 'fail') {
                        $('#message1').html(data.message);
                        setTimeout(function () {
                            $('#message1').html('');
                        }, 3000)
                    }
                }
            })
        }
        function onon() {
            var aa = document.getElementById('password')
            aa.type = 'text';
            alert('重置成功');
        }
    </script>
{% endblock %}